//////////////////////////////////////////////////////////////////////////////////
//	 Copyright 2011 by Yosef Grabivker, ryuho@homenetworksoft.com
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//////////////////////////////////////////////////////////////////////////////////

#include <wx/wx.h>
#include <wx/dcbuffer.h>
#include <wx/pen.h>
#include <wx/colordlg.h> 
#include "traficcolorselector.h"

DEFINE_EVENT_TYPE(wxEVT_COLOR_CHANGED)

CTraficColorSelector::CTraficColorSelector(wxWindow* parent, bool bRecv, wxWindowID id):
	wxPanel(parent, id,wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL),
	m_bRecv(bRecv)
	
{
	SetBackgroundStyle(wxBG_STYLE_CUSTOM);
	
	Connect(wxEVT_PAINT, wxPaintEventHandler(CTraficColorSelector::OnPaint));
	Connect(wxEVT_SIZE, wxSizeEventHandler(CTraficColorSelector::OnSize));
	Connect(wxEVT_LEFT_UP, wxMouseEventHandler(CTraficColorSelector::OnClick)); 

	SetMinSize(wxSize(36,36));

	m_color = bRecv?*wxBLUE:*wxRED;

	wxCursor curs( wxCURSOR_HAND );
	SetCursor(curs);
}

CTraficColorSelector::~CTraficColorSelector(void)
{
}

void CTraficColorSelector::SetColor(const wxColor&	 color)
{
	m_color = color;
	Refresh();
}

void CTraficColorSelector::OnClick(wxMouseEvent& event)
{
	wxColourData data;
	data.SetColour( m_color );

	wxColourDialog dlg(this, &data );

	if ( wxID_OK  == dlg.ShowModal() )
	{
		m_color = dlg.GetColourData().GetColour();
		Refresh();

		wxCommandEvent eventCustom( wxEVT_COLOR_CHANGED );
		eventCustom.SetId( GetId() );	
		wxPostEvent( GetParent(), eventCustom );
	}
}

void CTraficColorSelector::OnSize(wxSizeEvent& event)
{
	Refresh();
}

void CTraficColorSelector::OnPaint(wxPaintEvent& event)
{
	wxSize szMe = GetClientSize();
	wxBufferedPaintDC dc(this);

	wxBrush br(GetBackgroundColour());

	//Clear
	dc.SetBackground(br);
	dc.Clear();

	int sg = szMe.GetWidth()/4;

	szMe.x-=2;
	szMe.y-=2;

	wxPoint ptDown[7] = {
		wxPoint(sg, 0),
		wxPoint(szMe.GetWidth()-sg,0),
		wxPoint(szMe.GetWidth()-sg, szMe.GetHeight()/2-1),
		wxPoint(szMe.GetWidth(), szMe.GetHeight()/2-1),
		wxPoint(szMe.GetWidth()/2, szMe.GetHeight()-1),
		wxPoint(0, szMe.GetHeight()/2-1),
		wxPoint(sg, szMe.GetHeight()/2-1) };


	wxPoint ptUp[7] = {
		wxPoint(szMe.GetWidth()/2, 1),
		wxPoint(szMe.GetWidth(), szMe.GetHeight()/2+1),
		wxPoint(szMe.GetWidth()-sg, szMe.GetHeight()/2+1),
		wxPoint(szMe.GetWidth()-sg, szMe.GetHeight()-1),
		wxPoint(sg, szMe.GetHeight()-1),
		wxPoint(sg, szMe.GetHeight()/2+1),
		wxPoint(0, szMe.GetHeight()/2+1) };

	wxPoint* pt = m_bRecv?ptDown:ptUp;

	wxPen   pen(*wxBLACK, 1);
	wxBrush brush(m_color);

	dc.SetPen(pen);
	dc.SetBrush(brush);

	/*wxRegion reg(7, pt);

	dc.SetClippingRegion(reg);

	wxRect rc(szMe);

	unsigned char r = m_color.Red();
	unsigned char g = m_color.Green();
	unsigned char b = m_color.Blue();

	r*=1.01;
	g*=1.01;
	b*=1.01;

	wxColor colorFrom(r, g, b);

	dc.GradientFillLinear( rc, colorFrom, m_color );

	dc.DestroyClippingRegion();

	dc.SetBrush( *wxTRANSPARENT_BRUSH );*/

	dc.DrawPolygon(7, pt, 0, 0 );

	if ( m_bRecv )
		dc.DrawPolygon(7, pt, 1, 1 );
	else
		dc.DrawPolygon(7, pt, 1, -1 );
}
